package com.zoe.vhr.controller.system.basic;

import com.zoe.vhr.model.Position;
import com.zoe.vhr.service.PositionService;
import com.zoe.vhr.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author Zoe.Zhang
 * @Date: 2021/08/01
 * @Description
 */
@RestController
@RequestMapping("/system/basic/pos")
@Validated
public class PositionController {

    @Autowired
    PositionService positionService;

    @GetMapping("/listPositions")
    public R listPositions() {
        List<Position> positions = positionService.listPositions();
        return R.success(positions);
    }

    @PostMapping("/savePosition")
    public R savePosition(@Valid @RequestBody Position position) {
        int count = positionService.savePosition(position);
        if (count > 0) {
            return R.success("添加成功");
        }
        return R.failed("添加失败");
    }

    @PutMapping("/updatePosition")
    public R updatePosition(@Valid @RequestBody Position position) {
        int count = positionService.updatePosition(position);
        if (count > 0) {
            return R.success(count);
        }
        return R.failed("修改失败");
    }

    @DeleteMapping("/deletePosition/{id}")
    public R deletePosition(@NotNull @PathVariable("id") Integer id) {
        int count = positionService.deletePosition(id);
        if (count > 0) {
            return R.success(count);
        }
        return R.failed("删除失败");
    }

    /**
     * postman上面测试url {ids}直接传数组会报异常 得 1,2,3 这样传递数据
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatchPosition/{id}")
    public R deleteBatchPosition(@NotNull @PathVariable("id") List<Integer> ids) {
        int count = positionService.deleteBatchPosition(ids);
        if (count > 0) {
            return R.success(count);
        }
        return R.failed("批量删除失败");
    }
}
